import tkinter as tk
from tkinter import filedialog, messagebox, Listbox
import pandas as pd
import random

class RandomRollCallApp:
    def __init__(self, root):
        self.root = root
        self.root.title("科技风格随机点名系统")
        self.root.geometry("600x400")
        self.root.configure(bg="#1E1E1E")

        # 创建UI元素
        self.create_widgets()

    def create_widgets(self):
        # 标题
        title_label = tk.Label(self.root, text="随机点名系统", font=("Arial", 24, "bold"), fg="#00FF00", bg="#1E1E1E")
        title_label.pack(pady=20)

        # 上传CSV按钮
        upload_button = tk.Button(self.root, text="上传CSV文件", command=self.upload_csv, font=("Arial", 14), fg="#00FF00", bg="#333333", activebackground="#444444", activeforeground="#00FF00")
        upload_button.pack(pady=10)

        # 名单列表框
        self.listbox = Listbox(self.root, font=("Arial", 14), fg="#00FF00", bg="#333333", selectbackground="#444444", selectforeground="#00FF00")
        self.listbox.pack(pady=10, fill=tk.BOTH, expand=True)
        self.listbox.bind("<Double-Button-1>", self.select_list)

        # 点名按钮
        self.roll_button = tk.Button(self.root, text="开始点名", command=self.roll_call, font=("Arial", 14), fg="#00FF00", bg="#333333", activebackground="#444444", activeforeground="#00FF00", state=tk.DISABLED)
        self.roll_button.pack(pady=10)

        # 显示点名结果的标签
        self.result_label = tk.Label(self.root, text="", font=("Arial", 20, "bold"), fg="#00FF00", bg="#1E1E1E")
        self.result_label.pack(pady=20)

    def upload_csv(self):
        file_path = filedialog.askopenfilename(filetypes=[("CSV Files", "*.csv")])
        if file_path:
            self.data = pd.read_csv(file_path)
            self.names = self.data.iloc[:, 0].tolist()  # 假设第一列是名字
            self.listbox.insert(tk.END, file_path.split("/")[-1])
            self.roll_button.config(state=tk.NORMAL)
            messagebox.showinfo("上传成功", "CSV文件已成功上传！")

    def select_list(self, event):
        selected_index = self.listbox.curselection()
        if selected_index:
            selected_file = self.listbox.get(selected_index[0])
            file_path = filedialog.askopenfilename(filetypes=[("CSV Files", "*.csv")], initialfile=selected_file)
            if file_path:
                self.data = pd.read_csv(file_path)
                self.names = self.data.iloc[:, 0].tolist()  # 假设第一列是名字
                self.roll_button.config(state=tk.NORMAL)
                messagebox.showinfo("名单已应用", f"已应用名单: {selected_file}")

    def roll_call(self):
        if hasattr(self, 'names') and self.names:
            selected_name = random.choice(self.names)
            self.result_label.config(text=selected_name)
        else:
            messagebox.showwarning("警告", "请先上传CSV文件！")

if __name__ == "__main__":
    root = tk.Tk()
    app = RandomRollCallApp(root)
    root.mainloop()